//
//  LoadingViewController.swift
//  EasyDana
//
//  Created by mac on 2024/10/9.
//  引导页

import UIKit
import SnapKit
import RxSwift
import RxCocoa
class LoadingViewController: UIViewController {

    let bag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.configView()
        // Do any additional setup after loading the view.
    }
    
    private func configView() {
        let scrollView = UIScrollView()
        self.view.addSubview(scrollView)
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        let imageView1 = UIImageView(image: UIImage(named: "loading1"))
        imageView1.contentMode = .scaleAspectFill
        imageView1.frame = self.view.bounds
        scrollView.addSubview(imageView1)
        let imageView2 = UIImageView(image: UIImage(named: "loading2"))
        imageView2.contentMode = .scaleAspectFill
        imageView2.frame = CGRect(origin: CGPoint(x: self.view.bounds.width, y: 0), size: self.view.bounds.size)
        scrollView.addSubview(imageView2)
        scrollView.contentSize = CGSize(width: 2 * self.view.bounds.size.width, height: self.view.bounds.height)
        let button = UIButton(type: .system)
        self.view.addSubview(button)
        button.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(self.view.snp.bottomMargin).offset(-20)
            make.width.equalTo(300)
            make.height.equalTo(48)
        }
        scrollView.rx.contentOffset
            .map { [weak self] point -> UIImage in
                if point.x < self!.view.frame.width {
                    return UIImage(named: "loading_next")!.withRenderingMode(.alwaysOriginal)
                } else {
                    return UIImage(named: "loading_start")!.withRenderingMode(.alwaysOriginal)
                }
            }
            .subscribe(onNext: { image in
                button.setImage(image, for: .normal)
            })
            .disposed(by: self.bag)
        button.rx.tap
            .subscribe { _ in
                if (scrollView.contentOffset.x < self.view.bounds.width) {
                    scrollView.setContentOffset(CGPoint(x: self.view.bounds.width, y: 0), animated: true)
                } else {
                    let mainVC = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateInitialViewController()!
                    let window = (UIApplication.shared.connectedScenes.first as? UIWindowScene)?.windows.first
                    let transaction = CATransition()
                    transaction.type = .fade
                    window?.rootViewController = mainVC
                    window?.layer.add(transaction, forKey: nil)
                    UserDefaults.standard.set(true, forKey: "notfresher")
                }
            }
            .disposed(by: self.bag)
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
